cmake_minimum_required(VERSION 3.6)  
project(Format7zR LANGUAGES C CXX)

set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DZ7_NO_CRYPTO")

#COMMON_OBJS
set(COMMON_SRC
	${CMAKE_CURRENT_LIST_DIR}/CPP/Common/CRC.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/Common/CrcReg.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/Common/IntToString.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/Common/LzFindPrepare.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/Common/NewHandler.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/Common/MyString.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/Common/StringConvert.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/Common/StringToInt.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/Common/MyVector.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/Common/MyWindows.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/Common/Wildcard.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/Common/UTFConvert.cpp
)

#WIN_OBJS
set(WIN_SRC
	${CMAKE_CURRENT_LIST_DIR}/CPP/Windows/FileDir.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/Windows/FileFind.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/Windows/FileIO.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/Windows/FileName.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/Windows/PropVariant.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/Windows/Synchronization.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/Windows/System.cpp
)

# 7ZIP_COMMON_OBJS
set(7ZIP_COMMON_SRC
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Common/CreateCoder.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Common/CWrappers.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Common/InBuffer.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Common/InOutTempBuffer.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Common/FilterCoder.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Common/LimitedStreams.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Common/MethodId.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Common/MethodProps.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Common/OutBuffer.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Common/ProgressUtils.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Common/PropId.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Common/StreamBinder.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Common/StreamObjects.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Common/StreamUtils.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Common/UniqBlocks.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Common/VirtThread.cpp
)

# AR_OBJS
set(AR_SRC
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Archive/ArchiveExports.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Archive/DllExports2.cpp
)

# AR_COMMON_OBJS
set(AR_COMMON_SRC
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Archive/Common/CoderMixer2.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Archive/Common/HandlerOut.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Archive/Common/InStreamWithCRC.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Archive/Common/ItemNameUtils.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Archive/Common/OutStreamWithCRC.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Archive/Common/ParseProperties.cpp
)

# 7Z_OBJS
set(7Z_SRC
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Archive/7z/7zCompressionMode.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Archive/7z/7zDecode.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Archive/7z/7zEncode.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Archive/7z/7zExtract.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Archive/7z/7zFolderInStream.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Archive/7z/7zHandler.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Archive/7z/7zHandlerOut.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Archive/7z/7zHeader.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Archive/7z/7zIn.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Archive/7z/7zOut.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Archive/7z/7zProperties.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Archive/7z/7zSpecStream.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Archive/7z/7zUpdate.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Archive/7z/7zRegister.cpp
)

# COMPRESS_OBJS
set(COMPRESS_SRC
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Compress/CodecExports.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Compress/Bcj2Coder.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Compress/Bcj2Register.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Compress/BcjCoder.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Compress/BcjRegister.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Compress/BranchMisc.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Compress/BranchRegister.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Compress/ByteSwap.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Compress/CopyCoder.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Compress/CopyRegister.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Compress/DeltaFilter.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Compress/Lzma2Decoder.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Compress/Lzma2Encoder.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Compress/Lzma2Register.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Compress/LzmaDecoder.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Compress/LzmaEncoder.cpp
	${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Compress/LzmaRegister.cpp
)

# COMPRESS_OBJS
set(C_SRC
	${CMAKE_CURRENT_LIST_DIR}/C/7zCrc.c
	${CMAKE_CURRENT_LIST_DIR}/C/7zCrcOpt.c
	${CMAKE_CURRENT_LIST_DIR}/C/7zStream.c
	${CMAKE_CURRENT_LIST_DIR}/C/Alloc.c
	${CMAKE_CURRENT_LIST_DIR}/C/Bcj2.c
	${CMAKE_CURRENT_LIST_DIR}/C/Bcj2Enc.c
	${CMAKE_CURRENT_LIST_DIR}/C/Bra.c
	${CMAKE_CURRENT_LIST_DIR}/C/Bra86.c
	${CMAKE_CURRENT_LIST_DIR}/C/BraIA64.c
	${CMAKE_CURRENT_LIST_DIR}/C/CpuArch.c
	${CMAKE_CURRENT_LIST_DIR}/C/Delta.c
	${CMAKE_CURRENT_LIST_DIR}/C/LzFind.c
	${CMAKE_CURRENT_LIST_DIR}/C/LzFindMt.c
	${CMAKE_CURRENT_LIST_DIR}/C/LzFindOpt.c
	${CMAKE_CURRENT_LIST_DIR}/C/Lzma2Dec.c
	${CMAKE_CURRENT_LIST_DIR}/C/Lzma2DecMt.c
	${CMAKE_CURRENT_LIST_DIR}/C/Lzma2Enc.c
	${CMAKE_CURRENT_LIST_DIR}/C/LzmaDec.c
	${CMAKE_CURRENT_LIST_DIR}/C/LzmaEnc.c
	${CMAKE_CURRENT_LIST_DIR}/C/MtCoder.c
	${CMAKE_CURRENT_LIST_DIR}/C/MtDec.c
	${CMAKE_CURRENT_LIST_DIR}/C/SwapBytes.c
	${CMAKE_CURRENT_LIST_DIR}/C/Threads.c
)

# 获取当前目录下要编译的源文件  
set(Format7zR_SRC ${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Bundles/Format7zR/StdAfx.cpp)

# 添加要编译的源文件  
if(BUILD_SHARED_LIBS)
	add_library(Format7zR SHARED ${Format7zR_SRC} 
	${COMMON_SRC} ${WIN_SRC} ${7ZIP_COMMON_SRC} ${AR_SRC} ${AR_COMMON_SRC} ${7Z_SRC} ${COMPRESS_SRC} ${C_SRC})
else()
	add_library(Format7zR STATIC ${Format7zR_SRC}
	${COMMON_SRC} ${WIN_SRC} ${7ZIP_COMMON_SRC} ${AR_SRC} ${AR_COMMON_SRC} ${7Z_SRC} ${COMPRESS_SRC} ${C_SRC})
endif()

# 安装库文件  
install(TARGETS Format7zR DESTINATION ${CMAKE_INSTALL_PREFIX}/lib)

file(COPY ${CMAKE_CURRENT_LIST_DIR}/C DESTINATION ${CMAKE_INSTALL_PREFIX}/include)
file(COPY ${CMAKE_CURRENT_LIST_DIR}/CPP DESTINATION ${CMAKE_INSTALL_PREFIX}/include)
list(FILTER HEAD_FILES EXCLUDE REGEX ".*StdAfx\.h$")

file(GLOB_RECURSE DEL_FILES
	${CMAKE_INSTALL_PREFIX}/include/*.cpp
	${CMAKE_INSTALL_PREFIX}/include/*.c
	${CMAKE_INSTALL_PREFIX}/include/*.mak
	${CMAKE_INSTALL_PREFIX}/include/*.rc
	${CMAKE_INSTALL_PREFIX}/include/*.dsp
	${CMAKE_INSTALL_PREFIX}/include/*.dsw
	${CMAKE_INSTALL_PREFIX}/include/*.gcc
	${CMAKE_INSTALL_PREFIX}/include/*.def
	${CMAKE_INSTALL_PREFIX}/include/*.ico
	${CMAKE_INSTALL_PREFIX}/include/*.txt
	${CMAKE_INSTALL_PREFIX}/include/*.manifest
)
file(REMOVE ${DEL_FILES})
file(REMOVE_RECURSE ${CMAKE_INSTALL_PREFIX}/include/CPP/7zip/Bundles/)

file(COPY ${CMAKE_CURRENT_LIST_DIR}/CPP/7zip/Bundles/Format7zR/StdAfx.h DESTINATION ${CMAKE_INSTALL_PREFIX}/include/CPP/7zip/Bundles/Format7zR)
